<?php

namespace App\Http\Controllers;


use App\Models\Activities;
use App\Models\Bestsellers;
use App\Models\BestsellersCategories;
use App\Models\CompanyProfile;
use App\Models\CultureWall;
use App\Models\Job;
use App\Models\NewBookRecommend;
use App\Models\Product;
use App\Models\Sector;
use App\Models\Slideshow;
use App\Models\Specials;
use App\Models\TradeActivities;
use App\Models\TradeRecommend;
use Illuminate\Http\Request;

class IndexController extends Controller
{
    public function getIndex()
    {

        $slideshows = Slideshow::select('id', 'title', 'summary', 'url', 'link', 'sort')
            ->orderBy('sort')
            ->get();

        $recommends = NewBookRecommend::select('id', 'pid', 'extra', 'sort')
            ->with(['product' => function($query) {
                $query->select('id', 'uuid', 'name', 'summary', 'cover','is_new');
            }])
            ->whereHas('product', function ($query) {
                $query->select('id')->where('auditing_status', 'online');
            })
            ->take(8)
            ->orderBy('sort')
            ->get();

        foreach ($recommends as $key => $recommend)
        {
            if ($recommend->product)
                unset($recommend->product->id);
            unset($recommend->pid);
            $recommends[$key] = $recommend;
        }

        $bestSellersCategories = BestsellersCategories::select('id', 'title')
            ->whereHas('bestSellers', function ($query) {
                $query->select('id', 'pid', 'sort', 'category')
                    ->whereHas('product', function ($query) {
                        $query->select('id')->where('auditing_status', 'online');
                    });
            })
            ->get();

        foreach ($bestSellersCategories as $key => $bestSellersCategory)
        {

            $bestSellersCategory->load(['bestSellers' => function ($query) {
                $query->select('id', 'pid', 'sort', 'category')
                    ->orderBy('created_at', 'desc')
                    ->take(3)
                    ->whereHas('product', function ($query) {
                        $query->select('id')->where('auditing_status', 'online');
                    })
                    ->with(['product' => function($q) {
                        $q->select('id', 'uuid', 'name', 'summary', 'cover','is_new');
                    }]);
            }]);
            $bestSellersCategories[$key] = $bestSellersCategory;
        }

        $data = null;
        foreach ($bestSellersCategories as $key => $bestSellersCategory)
        {
            $sort = 'bestseller' . $key;

            foreach ($bestSellersCategory->bestSellers as $k => $item)
            {
                if ($item->product)
                    unset($item->product->id);
                $bestSellersCategory->bestSellers[$k] = $item->product;
            }
            $bestSellersCategories[$key]->sort = $sort;
            $bestSellersCategories[$key] = $bestSellersCategory;
        }

        $topics = Specials::select('id', 'title', 'summary', 'url', 'image')
            ->where('status', 1)
            ->orderBy('sort')
            ->take(3)
            ->get();

        $activities = Activities::select('id', 'title', 'url', 'image', 'sort')
            ->where('status', 1)
            ->orderBy('id', 'desc')
            ->take(7)
            ->get()
            ->toArray();
        $trades = TradeRecommend::select('id', 'product_id')
            ->with(['product' => function ($query) {
                $query->select('id', 'uuid', 'name', 'summary', 'cover','is_new');
            }])
            ->whereHas('product', function ($query) {
                $query->select('id')->where('auditing_status', 'online');
            })
            ->orderBy('created_at', 'desc')
            ->take(5)
            ->get();

        foreach ($trades as $key => $trade)
        {
            unset($trade->product->id);
            $trades[$key] = $trade->product;
        }

        return view('index', [
            'slideshows' => $slideshows,
            'recommends' => $recommends,
            'bestSellersCategories' => $bestSellersCategories,
            'topics' => $topics,
            'activities' => $activities,
            'trades' => $trades
        ]);
    }

    public function getEnglishIndex()
    {
    	//版贸的轮播图
    	$slide_show = TradeActivities::select('id', 'title', 'summary', 'link', 'image', 'date_time')
    					->where('slideshow','1')
				    	->orderBy('updated_at')
				    	->limit(6)
				    	->get();
		//版贸的轮播图下面那排
		$trade_activities = TradeActivities::select('id', 'title', 'summary', 'link', 'image')
						->where('slideshow','0')
						->orderBy('updated_at')
						->limit(3)
						->get();
		//成果展示
		$trade_recommends = TradeRecommend::select('id', 'product_id')
            ->with(['product' => function ($q) {
                $q->select('id', 'uuid', 'name', 'cover', 'summary');
                $q->with(['details' => function($query) {
                    $query->select('id', 'product_id', 'grade');
                }]);
            }])
            ->whereHas('product', function ($q) {
                $q->select('id', 'uuid')->where('auditing_status', 'online');
            })
            ->take(7)
            ->get();

		$companyInfo = CompanyProfile::select('id', 'address', 'telephone', 'fax', 'email', 'summary')
            ->where('language', 'en')
            ->first();

    	return view('en.index',[
    	    'trade_activities' => $trade_activities,
            'slide_show' => $slide_show,
            'trade_recommends' => $trade_recommends,
            'companyInfo' => $companyInfo
        ]);
    }


    public function getAboutUs()
    {
        $company = CompanyProfile::select('id', 'name', 'petrel_image', 'petrel_publishers',  'province_image',
            'province_publishers', 'connection', 'explain', 'node_intro')
            ->where('language', 'cn')
            ->first();

        if (!$company)
            $company = new CompanyProfile();

        $cultures = CultureWall::select('id', 'image')->orderBy('created_at', 'desc')->take(6)->get();

        return view('about_us', ['company' => $company, 'cultures' => $cultures]);
    }

    public function getJoinUs(Request $request)
    {
        $jobId = $request->input('job_id');

        $sectors = Sector::select('id', 'name')
            ->whereHas('jobs', function ($query) {
                $query->select('id', 'sector_id');
            })
            ->with(['jobs' => function($q) {
                $q->select('id', 'name', 'sector_id');
            }])
            ->select('id', 'name')
            ->get();

        if (!$jobId && count($sectors) >= 1)
            $jobId = $sectors->toArray()[0]['jobs'][0]['id'];


        $job = null;
        $explains = [];
        if ($jobId)
        {
            $job = Job::select('id', 'name', 'sector_id', 'describe', 'explain', 'telephone')->find($jobId);
            $explains = explode(',', $job->explain);
        }

        return view('join_us', [
            'job' => $job,
            'sectors' => $sectors,
            'job_id' => $jobId,
            'explains' => $explains
        ]);
    }

}
